package jpa.entities;

import java.io.Serializable;
import java.util.GregorianCalendar;
import javax.persistence.CascadeType;
import javax.persistence.Entity;
import javax.persistence.GeneratedValue;
import javax.persistence.GenerationType;
import javax.persistence.Id;
import javax.persistence.ManyToOne;
import javax.persistence.Table;
import javax.persistence.Temporal;
import javax.persistence.TemporalType;
import jsp.Datetime;

/**
 * @author Remco Haszing
 */
@Entity
@Table(name = "Task")
public class Task implements Serializable {

    // properties
    @Id
    @GeneratedValue(strategy = GenerationType.AUTO)
    private Long id;
    private String name;
    @Temporal(TemporalType.DATE)
    private GregorianCalendar startDate;
    @Temporal(TemporalType.DATE)
    private GregorianCalendar endDate;
    @Temporal(TemporalType.DATE)
    private GregorianCalendar deadLine;
    @ManyToOne(cascade = CascadeType.PERSIST)
    private Project project;
    private int importance;
    private String details;
    private String notes;

    // constructors
    /**
     * This constructor is used by the JPA API and should not be used for
     * anything else.
     *
     * @deprecated Use {@link #Task(String, GregorianCalendar, Project,
     * Employee, int, String, String)} instead.
     */
    public Task() {
    }

    /**
     * Creates a new task. This constructor creates a new task and adds it to
     * the employee's task list.
     *
     * @param name The name of the task.
     * @param deadLine The deadline of the task.
     * @param project The project this task is relevant to.
     * @param employee The employee to add this task to.
     * @param importance The importance of this task.
     * @param details Details about this task.
     * @param notes Notes about this task.
     */
    public Task(String name, GregorianCalendar deadLine, Project project, Employee employee, int importance, String details, String notes) {
        this.name = name;
        this.deadLine = deadLine;
        this.project = project;
        this.importance = importance;
        this.details = details;
        this.notes = notes;
        employee.addTask(this);
    }

    // getters
    /**
     * @return The unique id number of this task.
     */
    public long getId() {
        return this.id;
    }

    /**
     * @return The name of this task.
     */
    public String getName() {
        return this.name;
    }

    /**
     * @return The start date of this task.
     */
    public GregorianCalendar getStartDate() {
        return this.startDate;
    }

    /**
     * @return The date this task has been finished.
     */
    public GregorianCalendar getEndDate() {
        return this.endDate;
    }

    /**
     * @return The deadline of this task.
     */
    public GregorianCalendar getDeadLine() {
        return this.deadLine;
    }

    /**
     * @return The project this task belongs to.
     */
    public Project getProject() {
        return this.project;
    }

    /**
     * @return The importance of this task.
     */
    public int getImportance() {
        return this.importance;
    }

    /**
     * @return Details about this task.
     */
    public String getDetail() {
        return this.details;
    }

    /**
     * @return Notes about this task.
     */
    public String getNotes() {
        return this.notes;
    }

    // setters
    /**
     * Sets the notes of this task. This method sets the notes of this task.
     *
     * @param notes The notes to be set.
     */
    public void setNotes(String notes) {
        this.notes = notes;
    }

    // methods
    /**
     * @return The state of this task as a string.
     */
    public String getState() {
        if (this.startDate == null) {
            return "Not started yet";
        }
        if (this.endDate != null) {
            return "Finished (" + Datetime.format(this.endDate, "%d-%m-%Y") + ")";
        }
        return "In process";
    }

    /**
     * Start this task.
     */
    public void start() {
        this.startDate = new GregorianCalendar();
    }

    /**
     * Finish this task.
     */
    public void finish() {
        this.endDate = new GregorianCalendar();
    }
}
